<?php

if(count($_POST) == 0) {
	$Bug = $Author = $Body = $rev = $Subject = $State = $Project = $Category = '';
	if($edit) {
		$Subject = $bug_details->get('Subject');
		$Subject = substr($Subject, 0, ($pos = strpos($Subject, "\n"))?$pos+1:strlen($Subject));
		$Author = $bug_details->get('Author');
		$Body = $Subject.strip_tags($bug_details->get('Body'));
		$Rev = $bug_details->rev();
		$State = $bug_details->get('State');
		$Project = $bug_details->get('Project');
		$Category = $bug_details->get('Category');
	}
	$head = $edit?"Editing Bug Number $id":'Add a Bug';
	$icon = shrink_get_icon($State);
?>
<hr>
<div class="bug">

<h4><span class="<?=$State?>"><?=$icon?></span> <?=$head?></h4>
<form method="post">
<div class="tags">
	<div class="metaList">
		<ul>
			<?php
				shrink_show_projects($Project, $count = 5);
			?>
		</ul>
	</div>
	<span class="new_type">
		<a href="#" id="new_project_link" onClick="toggleNew('project')">New Project</a>
		<input style="display:none" type="text" name="new_project" value="" id="new_project">
	</span>
	<div class="label">
		&nbsp;
		<!-- Project -->
	</div>

</div>

<div class="tags">
	<div class="metaList">
		<ul>
			<?php
				shrink_show_categories($Category, $count = 5);
			?>
		</ul>
	</div>
	<span class="new_type">
		<a href="#" id="new_category_link" onClick="toggleNew('category')">New Type</a>
		<input style="display:none" type="text" name="new_category" value="" id="new_category">
	</span>
	<div class="label">
		&nbsp;
		<!-- Type -->
	</div>
</div>
	<!-- <input type="text" name="Subject" value="<?=$Subject?$Subject:'Bug title';?>"> -->
	<div id="help">The first line becomes the Bug's title. You can use <a href="http://daringfireball.net/projects/markdown/" title="Markdown Homepage" target="_new">Markdown</a></div>
	<textarea id="BugBody" name="Body" cols="50" rows="16"><?=$Body?$Body:'Bug description';?></textarea>
	<div id="AuthorContainer">
		<span id="by">by:</span> 
		<?php if ($edit): ?>
			<?=$Author?>
			<input type="hidden" id="Author" name="Author" value="<?=$Author?$Author:'Your Name or Email';?>" onFocus="<?=$edit?'':'this.value=\'\';'?>">
		<?php else: ?>
			<input type="text" id="Author" name="Author" value="<?=$Author?$Author:'Your Name or Email';?>" onFocus="<?=$edit?'':'this.value=\'\';'?>">
		<?php endif; ?>
	</div>
		<input type="hidden" name="Project" id="Project" value="<?=$Project?>">
		<input type="hidden" name="Category" id="Category" value="<?=$Category?>">	
	<?php
	if($edit) {

		?>

		<input type="hidden" name="id" value="<?=$id?>">
		<input type="hidden" name="rev" value="<?=$Rev?>">
		<?
	}
	?>
	<input type="text" name="email" style="display:none;">
	<input type="submit" value="<?=$edit?'Save Changes':'Add Bug';?>">
</form>

	<?php
	if($edit) {
		$quickAction = ($bug_details->get('State') == 'Closed')?'Open':'Close';
	?>
		<a href="?action=<?=$quickAction?>&id=<?=$id?>"><?=$quickAction?> this Bug</a> 
		
	<?php } ?>

</div>

<?php	
} else {

function var_register_post($name)
{
	if(isset($_POST[$name])) {
		return strip_tags(htmlspecialchars($_POST[$name]));
	}
	
	return '';
}

function shrink_extract_subject($s)
{
	$offset = 1;

	if(!$pos = strpos($s, "\n")) {
		return $s;
	}
	
	if(isset($s[$pos+2]) && $s[$pos+2] == "\n") {// catch empty newlines
		$offset++;
	}
	
	return substr($s, 0, $pos + $offset); 
}

function shrink_extract_meta($Subject)
{
	$matches = array();
	preg_match('/\[([^\s]*)\s?([^\]]*)\]/', $Subject, $matches);
	return array($matches[1], $matches[2]);
}

if($spam = var_register('email')) {
	header('Location: index.php');
	die();
}

$id = var_register_post('id');
$revision =  var_register_post('rev');
$Body = var_register_post('Body');
$Project = var_register_post('Project');
$Category = var_register_post('Category');


$Subject = shrink_extract_subject(strip_tags($Body));
$Body = (str_replace(strip_tags($Subject), '', $Body));

if(($new_project = var_register_post('new_project')) && ($new_project != '')) {
	$Project = $new_project;
}

if(($new_category = var_register_post('new_category'))  && ($new_category != '')) {
	$Category = $new_category;
}

shrink_save_meta($Project, $Category);

$bug_details = $db->newDocument($id, $revision);
$bug_details->set('Subject', $Subject);
$bug_details->set('_rev', $revision);
$bug_details->set('Body', $Body);

$bug_details->set('CreationDate', date('Y-m-d:H:i:s'));

$bug_details->set('Author', var_register_post('Author'));


$bug_details->set('Project', ($Project));
$bug_details->set('Category', ($Category));

//$bug_details->attach('Cat.jpg', base64_encode(file_get_contents('/Users/jan/Pictures/14788373_a47e5096d2_m.jpg')));



$state = var_register_post('State');
$bug_details->set('State', empty($state)?'Open':$state);
// var_dump($_POST);
if(!$bug_details->save()) {
	//if revision conflict, show diff:
	if($couch->lastStatusCode == 409) { 
		// $lastRev = $couch->get($id, $options = array("rev" => $revision));
		// $currRev = $couch->get($id);
		// //generalize diff, file_put.., tmpfile
		// file_put_contents('/tmp/bs_diff_1', $lastRev->get('Body'));
		// file_put_contents('/tmp/bs_diff_2', $currRev->get('Body'));
		// $diff = `/usr/bin/diff -u /tmp/bs_diff_1 /tmp/bs_diff_2`;
		// if(strlen($diff) > 0) {
		// 	<h3>There was a revision conflict. See below for a diff. Save anyway?</h3><?
		// 	echo '<pre id="diff">'.( nl2br($diff) ). '</pre>';
		// }
	} else {
		// var_dump($bug_details);
		echo 'Failed to save your changes, sorry!';
	}
}
// header('Location: index.php');
$action = ''; //unset action
}
